<?php /* PROJECTS $Id: vw_calendar_by_user.php,v 1.2 2007/01/31 16:02:50 theideaman Exp $ */
/**
* Modified and adapted by Jonathan Dumaresq on 2005/03/08
* Generates a report of the task logs for given dates for the logged user
*/
error_reporting( E_ALL );

global $dPconfig;

$do_report = dPgetParam( $_POST, 'do_report', 0 );
$log_pdf = dPgetParam( $_POST, 'log_pdf', 0 );
$log_start_date = dPgetParam( $_POST, 'log_start_date', 0 );
$log_end_date = dPgetParam( $_POST, 'log_end_date', 0 );
// create Date objects from the datetime fields
$start_date = intval( $log_start_date ) ? new CDate( $log_start_date ) : new CDate();
$end_date = intval( $log_end_date ) ? new CDate( $log_end_date ) : new CDate();

$df = $AppUI->getPref('SHDATEFORMAT');

$AppUI->savePlace();

if (!$log_start_date) {
	$start_date->subtractSpan( new Date_Span( "14,0,0,0" ) );
}
$end_date->setTime( 23, 59, 59 );

?>
<script language="javascript" type="text/javascript">
Calendar.enabled = true;
</script>

<form name="editFrm" action="index.php?m=timecard&tab=<?=$currentTabId?>" method="post">

<table cellspacing="3" cellpadding="0" border="0" width="100%" class="infopanel">
<tr>
	<td nowrap="nowrap">
		<input class="button" type="submit" name="do_report" value="<?php echo $AppUI->_('create');?>" />&nbsp;
	</td>
	<td align="right" nowrap="nowrap"><?php echo $AppUI->_('For period');?>:</td>
	<td nowrap="nowrap">
		<input id="idDateStart" type="hidden" name="log_start_date"
				value="<?php echo $start_date->format( FMT_TIMESTAMP_DATE );?>" />
		<input id="idDateStartD" type="text" name="start_date"
				value="<?php echo $start_date->format( $df );?>"
				class="text" disabled="disabled" style="width: 80px" />
	</td>
	<td>
		<img id="idDateStartB" align="absmiddle" src="images/calendar.gif"
			title="<?php echo $AppUI->_('Calendar');?>"/>
	</td>
	<td align="right" nowrap="nowrap">&nbsp;<?php echo $AppUI->_('to');?>&nbsp;</td>
	<td nowrap="nowrap">
		<input id="idDateEnd" type="hidden" name="log_end_date"
				value="<?php echo $end_date ? $end_date->format( FMT_TIMESTAMP_DATE ) : '';?>" />
		<input id="idDateEndD" type="text" name="end_date"
				value="<?php echo $end_date ? $end_date->format( $df ) : '';?>"
				class="text" disabled="disabled" style="width: 80px"/>
	</td>
	<td>
		<img id="idDateEndB" align="absmiddle" src="images/calendar.gif"
			title="<?php echo $AppUI->_('Calendar');?>"/>
	</td>

	<td width="10px" nowrap="nowrap" valign="middle">&nbsp;&nbsp;
		<input id="pdf" type="checkbox" name="log_pdf" <?php if ($log_pdf) echo "checked" ?> />
	</td>
	<td width="100%" nowrap="nowrap"><label for="pdf"><?php echo $AppUI->_( 'Make PDF' );?></label></td>
</tr>
</form>
</table>
<?php
if ($do_report) {

	$sql = "SELECT tl.*,
					t.task_id, u.user_id,
					p.project_short_name, p.project_name, p.project_description,
					c.contact_first_name, c.contact_last_name
		FROM
			task_log AS tl
			LEFT JOIN tasks AS t ON tl.task_log_task = t.task_id
			LEFT JOIN projects AS p ON p.project_id = t.task_project
			INNER JOIN users AS u ON u.user_id = tl.task_log_creator
			LEFT JOIN contacts AS c ON c.contact_id = u.user_contact
		WHERE "
		." task_log_date >= '".$start_date->format( FMT_DATETIME_MYSQL )."' AND"
		." task_log_date <= '".$end_date->format( FMT_DATETIME_MYSQL )."'"
		." ORDER BY contact_first_name, task_log_creator, task_log_date, project_short_name";

	$logs = db_loadList( $sql );
	echo db_error();

	$base_url  = $dPconfig['base_url'];
?>
	<table width=100% cellspacing="1" cellpadding="4" border="0" class="list">
	<tr>
		<th nowrap="nowrap" style="padding: 2px" align="center" width="1%">
		<? if ($log_pdf) { ?>
			<img class="clickable" src="images/pdf.png" title="View PDF report" onclick="go('<?=$base_url?>/files/temp/temp<?=$AppUI->user_id?>.pdf', 'pdf')">
			<!--img src="images/excel.gif" title="Excel report" onclick="go('<?=$base_url?>/files/temp/temp<?=$AppUI->user_id?>.xls', 'xls')"-->
		<? } else { ?>
			<img src="images/report.png"/>
		<? } ?>
		</th>
		<th width="10%" nowrap="nowrap">
		<?php echo $AppUI->_('Date');?></th>
		<th width="7%"><?php echo $AppUI->_('Project');?></th>
		<th width="11%"><?php echo $AppUI->_('Task');?></th>
		<th width="70%"><?php echo $AppUI->_('Description');?></th>
		<th class="last" width="1%"><?php echo $AppUI->_('Hours');?></th>
	</tr>

<?php

	$pdetail = array();
	$pdfdata = array();
	$subtotal = 0.0;
	$total_hours = 0.0;
	$uname = '';
	$cdate = '';
	if (count($logs) == 0)
		showEmptyRow(5, 'second');
	else
	foreach ($logs as $log) {
		$project_name = $log['project_short_name'];
		$pdetail[$project_name] = $log['project_name'].'  --  '.$log['project_description'];
		$date = new CDate( $log['task_log_date'] );
		if ($uname != $log['contact_first_name'].' '.$log['contact_last_name']) {
			if ($uname != '') {
				echo '<tr class="second"><td colspan="5" align="right"><b>Sub total:</b></td>'.
						'<td align="right"><b>'.sprintf( "%.2f", $subtotal ).'<b></td></tr>';
				$pdfdata[] = array('','','','','<r><b>Sub total</b>','<b>'.sprintf( "%.2f", $subtotal ).'</b>');
				$subtotal = 0;
			}
			$uname = $log['contact_first_name'].' '.$log['contact_last_name'];
			echo '<tr><td colspan="6"><b>'.$uname.'</b></td></tr>';
			$pdfdata[] = array('<s><b>'.$uname.'</b>','','','','','');
			$cdate = '';
		}

		$d = $date->format( $df );
		if ($cdate != $d) {
			$cdate = $d;
		}
		else {
			$d = '';
		}

		$hours = floatval($log['task_log_hours']);
		$total_hours += $hours;
		$subtotal += $hours;

		$pdfdata[] = $data = array(
			'',
			$d,
			$project_name,
			html_entity_decode($log['task_log_name']),
			$log['task_log_description'],
			sprintf( "%.2f", $hours ) //.' '.sprintf( "%.2f", $subtotal )
		);

?>
	<tr class="second" title="<?=htmlentities($pdetail[$data[2]])?>">
		<td nowrap="nowrap" valign="top" colspan="2" align="right">
			<?=$data[1]?>
		</td>
		<td nowrap="nowrap" valign="top">
			<?=$data[2]?>
		</td>
		<td width="40%" valign="top">
			<?=$data[3]?>
		</td>
		<td valign="top">
			<?=str_replace("\n", '<br/>', $data[4])?>
		</td>
		<td nowrap="nowrap" valign="top" align="right">
			<?=$data[5]?>
		</td>
	</tr>
<?php
	}

	if ($uname != '') {
		echo '<tr class="second"><td colspan="5" align="right"><b>Sub total:</b></td>'.
				'<td align="right"><b>'.sprintf( "%.2f", $subtotal ).'<b></td></tr>';
		$pdfdata[] = array('','','','','<r><b>Sub total</b>','<b>'.sprintf( "%.2f", $subtotal ).'</b>');
		$subtotal = 0;
	}

	$pdfdata[] = array(
		'',
		'',
		'',
		'',
		'<r><b>'.$AppUI->_('Total Hours').'</b>',
		'<b>'.sprintf( "%.2f", $total_hours ).'</b>',
		'',
	);

	if ($log_pdf) {
		$font_dir = $dPconfig['root_dir']."/lib/ezpdf/fonts";
		$temp_dir = $dPconfig['root_dir']."/files/temp";
		require( $AppUI->getLibraryClass( 'ezpdf/class.ezpdf' ) );

		$pdf =& new Cezpdf('a4', 'landscape');
		$pdf->setPreferences('HideToolbar', 1);
		$pdf->ezSetCmMargins( 1, 2, 1.5, 1.5 );
		$pdf->selectFont( "$font_dir/Helvetica-Bold.afm" );

		$pdf->ezText( $dPconfig['company_name'], 14 );

		$date = new CDate();
		$pdf->ezText( "\nReport date: " . $date->format( $df ) , 8 );

		$pdf->ezText( "\n" . $AppUI->_('Timesheet Report'), 18, array('justification' => 'center') );

		$pdf->ezText( "\n\nTask log entries from ".$start_date->format( $df ).' to '.$end_date->format( $df ), 9 );
		$pdf->ezText( "\n" );

		$pdf->selectFont( "$font_dir/Helvetica.afm" );

		$columns = array(
			'',
			$AppUI->_('Date'),
			$AppUI->_('Project'),
			$AppUI->_('Task'),
			$AppUI->_('Comments'),
			$AppUI->_('Hours')
		);
		$title = null;
		$options = array(
			'showLines' => 2,
			'showHeadings' => 1,
			'shaded'=> 1,
			'shadeCol' => array(0.9, 0.9, 0.9),
			'fontSize' => 9,
			'rowGap' => 2,
			'colGap' => 5,
			'xPos' => 50,
			'xOrientation' => 'right',
			'width'=>'750',
			'col_options' => array(
				0 => array('width' => 100),
				1 => array('width' => 60),
				2 => array('width' => 80),
				5 => array(
					'justification' => 'right',
					'width' => 40
				)
			)
		);

		$pdf->ezTable( $pdfdata, $columns, $title, $options );

		ksort($pdetail);
		$pdf->ezText( "\n" . $AppUI->_('Legend') . "\n", 12 );

		$n = 1;
		$legends = array();
		foreach($pdetail as $ps => $pn) {
			$legends[] = array($n++, $ps, html_entity_decode($pn));
		}
		$pdf->ezTable( $legends, null, null,
			array(
				'showLines' => 0,
				'showHeadings' => 0,
				'shaded' => 0,
				'fontSize' => 10,
				'rowGap' => 3,
				'colGap' => 5,
				'xPos' => 50,
				'xOrientation' => 'right',
				'width'=>'750',
				'col_options' => array(
					0 => array('width' => 30),
					1 => array('width' => 100),
					2 => array('width' => 600),
				)
			));

		if ($fp = fopen( "$temp_dir/temp$AppUI->user_id.pdf", 'wb' )) {
			fwrite( $fp, $pdf->ezOutput() );
			fclose( $fp );
		} else {
			echo "Could not open file to save PDF.  ";
			if (!is_writable( $temp_dir )) {
				"The files/temp directory is not writable.  Check your file system permissions.";
			}
		}
	}
?>
	<tr>
		<td align="right" colspan="5"><b><?php echo $AppUI->_('Total Hours');?>:</b></td>
		<td align="right"><b><?php printf( "%.2f", $total_hours );?></b></td>
	</tr>
	</table>
<? } // do_report ?>
